package com.glsc.ngateway.platform.controller.glres;


import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemAccount;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemResource;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.SystemMain;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.dto.GlscSystemAccountDto;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.service.glres.GlscSystemAccountService;
import com.glsc.ngateway.platform.service.glres.GlscSystemResService;
import com.glsc.ngateway.platform.service.system.SystemMainService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 资源管理-系统资源账号
 */
@RestController
@RequestMapping("/api/res/system/account")
@Slf4j
public class GlscSystemAccountController {

    @Resource
    private UserCache userCache;

    @Resource
    private GlscSystemAccountService accountService;

    @Resource
    private GlscSystemResService glscSystemResService;

    @Resource
    private UserService userService;

    @Resource
    private SystemMainService systemMainService;

    /**
     * 分页查找
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<GlscSystemAccount>> page(@RequestBody GlscSystemAccountDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getGlscAccount())) {
            param.put("LIKE_glscAccount", dto.getGlscAccount());
        }
        if (StringUtils.isNotBlank(dto.getAccountType())) {
            param.put("EQ_accountType", dto.getAccountType());
        }
        if (StringUtils.isNotBlank(dto.getUserOaAccount())) {
            param.put("EQ_userOaAccount", dto.getUserOaAccount());
        }

        if (Objects.isNull(dto.getSystemId())) {
            PlatformResponse.successData(Page.empty());
        }
        param.put("EQ_systemId", dto.getSystemId());

        param.put("EQ_sourceType", dto.getSourceType());

        Page<GlscSystemAccount> page = accountService.page(dto.getPageNo(), dto.getPageSize(), param);
        List<GlscSystemAccount> content = page.getContent();
        if (CollectionUtils.isNotEmpty(content)) {
            if ("2".equals(dto.getSourceType())) {
                List<GlscSystemResource> glscSystemResources = glscSystemResService.listAllSimpleInfo(null);
                if (CollectionUtils.isNotEmpty(glscSystemResources)) {
                    Map<Integer, String> idNameMap = glscSystemResources.stream().collect(Collectors.toMap(GlscSystemResource::getId, GlscSystemResource::getName, (i, j) -> j));
                    for (GlscSystemAccount glscSystemAccount : content) {
                        if (Objects.nonNull(glscSystemAccount.getAccountSystemId())) {
                            glscSystemAccount.setSystemName(idNameMap.get(glscSystemAccount.getAccountSystemId()));
                        }
                    }
                }
            } else {
                for (GlscSystemAccount glscSystemAccount : content) {
                    if (Objects.nonNull(glscSystemAccount.getAccountSystemId())) {
                        SystemMain systemMain = systemMainService.findSimpleInfoById(glscSystemAccount.getSystemId());
                        if (Objects.nonNull(systemMain)) {
                            glscSystemAccount.setSystemName(systemMain.getOpName());
                        }
                    }
                }
            }
        }

        return PlatformResponse.successData(page);
    }

    /**
     * 根据id查找
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<GlscSystemAccount> findById(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(accountService.findById(id));
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<?> save(@RequestBody GlscSystemAccount obj, HttpServletRequest request) {

        PlatformUserDto userDto = userService.findByUsername(obj.getUserOaAccount());
        obj.setUserId(userDto.getId());
        obj.setUserName(userDto.getName());
        accountService.save(obj);
        return PlatformResponse.success();
    }

    /**
     * 根据地删除
     *
     * @param obj
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody GlscSystemAccount obj) {
        accountService.delete(obj.getId());
        return PlatformResponse.success();
    }


}
